/**
 * 
 */
package edu.umd.clip.lm.model.decoding;

import java.util.*;

import edu.umd.clip.lm.model.decoding.ViterbiTagger.ViterbiPath;
import edu.umd.clip.lm.util.BinaryPrefix;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class ViterbiTaggerContext {
	private BinaryPrefix tags[];
	private Map<BinaryPrefix, Integer> prefixTags;
	TreeSet<ViterbiPath> currentPaths;

	public ViterbiTaggerContext(BinaryPrefix[] tags, TreeSet<ViterbiPath> currentPaths) {
		this.tags = tags;
		this.currentPaths = currentPaths;
		this.prefixTags = Collections.synchronizedMap(new HashMap<BinaryPrefix, Integer>());
	}

	public ViterbiTaggerContext(BinaryPrefix[] tags) {
		this(tags, new TreeSet<ViterbiPath>());
	}

	public int getTag(BinaryPrefix prefix) {
		Integer tag = prefixTags.get(prefix);
		if (tag == null) {
			for(int i=0; i<tags.length; ++i) {
				if (tags[i] != null && tags[i].isPrefixOf(prefix)) {
					prefixTags.put(prefix, i);
					return i;
				}
			}
			return -1;
		}
		return tag.intValue();
	}
}
